/*********************************************************************************************************
**
**                                   GJB ��׼���Լ�
**
**                               Copyright All Rights Reserved
**
**--------------�ļ���Ϣ--------------------------------------------------------------------------------
**
** ��   ��   ��: gjb_S0100904GN_4.c
**
** �ļ���������: 2021 �� 1 �� 22 ��
**
** ��        ��: ʹ����������ͬʱ����һ�����߶���ȴ�����?/���̹��ܲ���
*********************************************************************************************************/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

// #include "gjb.h"

static int err_count_cv_9044 = 0;
static pthread_cond_t cond1_cv_9044 = PTHREAD_COND_INITIALIZER;                  /* cond used by the two threads */
static pthread_mutex_t mutex2_cv_9044 = PTHREAD_MUTEX_INITIALIZER;               /* cond used by the two threads */

void * thread2_cv_9044(void *tmp)
{
    int    rc = 0;

    /*
     * acquire the mutex
     */
    rc  = pthread_mutex_lock(&mutex2_cv_9044);
    if (rc != 0) {
        printf("test failed.pthread_mutex_lock\n");
        err_count_cv_9044++;
        return NULL;
    }

    /*
     * Wait on the cond var. This will not return, as nobody signals
     */
    rc = pthread_cond_wait(&cond1_cv_9044, &mutex2_cv_9044);
    if (rc != 0) {
        printf("test failed.pthread_cond_wait\n");
        err_count_cv_9044++;
        return NULL;
    }


    rc = pthread_mutex_unlock(&mutex2_cv_9044);
    if (rc != 0) {
        printf("test failed.pthread_mutex_unlock\n");
        err_count_cv_9044++;
        return NULL;
    }

    return NULL;
}

// int gjb_S0100904GN_4(int argc, char **argv)
int main(int argc, char **argv)
{
    pthread_t low_id;
    int       rc = 0;

    /*
     * Create a new thread with default attributes
     */
    rc = pthread_create(&low_id, NULL, thread2_cv_9044, NULL);
    if (rc != 0) {
        printf("pthread_create\n");
        goto __errno_handle;
    }

    /*
     * Let the other thread run
     */
    sleep(2);

    rc = pthread_cond_broadcast(&cond1_cv_9044);
    if (rc == 0) {
        printf("test illegal pass.pthread_cond_broadcast return EINVAL\n");

    } else {
        printf("test failed:pthread_cond_broadcast should return EINVAL,but return %d\n",rc);
        goto __errno_handle;
    }

    rc = pthread_cond_broadcast(NULL);
    if (rc == EINVAL) {
        printf("test NULL pass.pthread_cond_broadcast return EINVAL\n");

    } else {
        printf("test failed:pthread_cond_broadcast should return EINVAL,but return %d\n",rc);
        goto __errno_handle;
    }

    if (err_count_cv_9044) {
        goto __errno_handle;
    }
    printf("<RSLT>..................................................[(0)]\n \n \n");

    return  (0);      

__errno_handle:
    printf("<RSLT>..................................................[(-1)]\n \n \n");
    return  (-1);  

}
